/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

import config.Debug;
import config.Helper;
import config.Settings;

/**
 * 
 * @author Yang Wang <yang@digitalmenubox.com>
 */
public class Container {
	private int id;
	private String name;
	private int left;
	private int top;
	private int width;
	private int height;
	private int depth;
	private int pageId;
	


	private PlaylistRecord currentPlaylistRecord;

	public PlaylistRecord getCurrentPlaylistRecord() {
		return currentPlaylistRecord;
	}

	public void setCurrentPlaylistRecord(PlaylistRecord currentPlaylistRecord) {
		this.currentPlaylistRecord = currentPlaylistRecord;
	}

	public Container(int id) {
		assert (id > 0);
		this.createContainerFromDatabaseById(id);
	}

	public static boolean containerExists(int id) {
		String sql = String.format("SELECT * FROM `container` WHERE `container_id`='%1$s';", id);
		Connection conn = Settings.getLocalDatabaseConnection();
		ResultSet result = null;
		Statement stmt = null;
		try {
			int numResults = Helper.getNumberOfResults(sql);
			if (numResults <= 0) {
				// throw new
				// com.mysql.jdbc.exceptions.MySQLDataException("There is no container with id: "
				// + id);
				// Debug.log("There is no container with id: " + id,
				// Debug.WARNING_LEVEL);
				return false;
			} else {
				return true;
			}
		} catch (Exception e) {
			Debug.log(e.getMessage(), Debug.EXCEPTION_LEVEL);
		} finally {
			try {
				if (result != null)
					result.close();
				if (stmt != null)
					stmt.close();
			} catch (SQLException ex) {
				Logger.getLogger(Container.class.getName()).log(Level.SEVERE, null, ex);
			}

		}
		return false;
	}

	private void createContainerFromDatabaseById(int id) {
		String sql = String.format("SELECT * FROM `container` WHERE `container_id`='%1$s';", id);
		Connection conn = Settings.getLocalDatabaseConnection();
		ResultSet result = null;
		Statement stmt = null;
		try {
			int numResults = Helper.getNumberOfResults(sql);
			if (numResults <= 0) {
				// throw new
				// com.mysql.jdbc.exceptions.MySQLDataException("There is no container with id: "
				// + id);
				Debug.log("There is no container with id: " + id, Debug.WARNING_LEVEL);
				return;
			}
			stmt = conn.createStatement();
			result = stmt.executeQuery(sql);
			result.first();
			this.id = id;
			this.name = result.getString("name");
			this.left = Helper.scale(result.getInt("left"));
			this.top = Helper.scale(result.getInt("top"));
			this.width = Helper.scale(result.getInt("width"));
			this.height = Helper.scale(result.getInt("height"));
			this.depth = result.getInt("z_index");
			this.pageId = result.getInt("page_id_fk");
		} catch (SQLException ex) {
			Logger.getLogger(Container.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				if (result != null)
					result.close();
				if (stmt != null)
					stmt.close();
			} catch (SQLException ex) {
				Logger.getLogger(Container.class.getName()).log(Level.SEVERE, null, ex);
			}

		}

		result = null;
	}

	public int getDepth() {
		return depth;
	}

	public int getHeight() {
		return height;
	}

	public int getId() {
		return id;
	}

	public int getLeft() {
		return left;
	}

	public String getName() {
		return name;
	}

	public int getTop() {
		return top;
	}

	public int getWidth() {
		return width;
	}

	@Override
	public String toString() {
		return "[Container " + this.getId() + "]";
	}

	public int getPageId() {
		return pageId;
	}

	public void setPageId(int pageId) {
		this.pageId = pageId;
	}

	private PlaylistRecord getCurrentRecord() {
		// return this.records.peek();
		return null;
	}

	public static void main(String[] args) {
		Container c = new Container(4);
	}

}
